package com.workingpub.commons.utils;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;

/**
 * 主键生成器
 * 
 * @author WangXianbing
 * @version 1.0
 * @date 2014年9月3日-下午2:26:01
 */
public class Guid {

	private String m_strValueBeforeMD5 = "";
	private String m_strValueAfterMD5 = "";
	private static Random m_myRand = null;
	private static SecureRandom m_mySecureRand = null;

	private static final String Guid_Algorithm = "MD5";
	private static final String Append_Separator = ":";
	private static final String ToString_Separator = "-";
	private static final String Empty = "";

	private static String m_strSID = null;

	static {
		m_mySecureRand = new SecureRandom();
		long secureInitializer = m_mySecureRand.nextLong();
		m_myRand = new Random(secureInitializer);
		try {
			m_strSID = InetAddress.getLocalHost().toString();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	private String getValueAfterMD5() {
		return m_strValueAfterMD5;
	}

	private Guid() {
		getRandomGUID(false, null);
	}

	private Guid(boolean secure, String salt) {
		getRandomGUID(secure, salt);
	}

	private void getRandomGUID(boolean secure, String salt) {
		try {
			StringBuffer sbValueBeforeMD5 = new StringBuffer();
			MessageDigest md5 = MessageDigest.getInstance(Guid_Algorithm);
			long time = System.currentTimeMillis();
			long rand = 0;

			if (secure) {
				rand = m_mySecureRand.nextLong();
			} else {
				rand = m_myRand.nextLong();
			}

			if (null != salt && salt.trim().length() > 0) {
				sbValueBeforeMD5.append(salt.trim());
			}

			// This StringBuffer can be a long as you need; the MD5
			// hash will always return 128 bits. You can change
			// the seed to include anything you want here.
			// You could even stream a file through the MD5 making
			// the odds of guessing it at least as great as that
			// of guessing the contents of the file!
			sbValueBeforeMD5.append(m_strSID);
			sbValueBeforeMD5.append(Append_Separator);
			sbValueBeforeMD5.append(Long.toString(time));
			sbValueBeforeMD5.append(Append_Separator);
			sbValueBeforeMD5.append(Long.toString(rand));

			m_strValueBeforeMD5 = sbValueBeforeMD5.toString();
			md5.update(m_strValueBeforeMD5.getBytes());

			byte[] array = md5.digest();
			StringBuffer sb = new StringBuffer();
			for (int j = 0; j < array.length; ++j) {
				int b = array[j] & 0xFF;
				if (b < 0x10) {
					sb.append(BigDecimal.ZERO.toString());
				}
				sb.append(Integer.toHexString(b));
			}

			m_strValueAfterMD5 = sb.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	/*
	 * Convert to the standard format for GUID (Useful for SQL Server
	 * UniqueIdentifiers, etc.) Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
	 */
	@Override
	public String toString() {
		String raw = m_strValueAfterMD5.toUpperCase();
		StringBuffer sb = new StringBuffer();
		sb.append(raw.substring(0, 8));
		sb.append(ToString_Separator);
		sb.append(raw.substring(8, 12));
		sb.append(ToString_Separator);
		sb.append(raw.substring(12, 16));
		sb.append(ToString_Separator);
		sb.append(raw.substring(16, 20));
		sb.append(ToString_Separator);
		sb.append(raw.substring(20));
		return sb.toString();
	}

	public static String newGuid(String salt) {
		return newGuid(salt, null);
	}

	public static String newGuid(String salt, String strPrefix) {
		return (null == strPrefix || strPrefix.trim().length() < 1 ? Empty : strPrefix.trim())
				+ (new Guid(true, salt)).getValueAfterMD5();
	}

	public static String newGuid() {
		return newGuid(null, null);
	}

}
